;$(function () {
    layui.use(['form', 'laydate', 'table', 'element'], function () {
        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            $ = layui.jquery,
            element = layui.element;

        let nowDate = getNowAndFormatDate();
        let now = {};
        now[nowDate] = "今天";
        let weekDate = getWeekAndFormatDate();
        let showDate = weekDate + " 00:00:00 - " + nowDate + " 23:59:59";
        laydate.render({
            elem: '#damageReportDate'
            , range: true
            , trigger: 'click'
            , type: 'datetime'
            , mark: now
            , value: showDate
            , done: function (value) {
                $("#damageReportDate").val(value);
                $("#damageReportSearchBtn").click();
            }
        });
        getDamageReportEChartsData();
        $(".layui-btn.layui-btn-primary.dateChoose").on('click', function () {
            let event = $(this).attr("lay-event");
            let dateValue;
            let nowDate = getNowAndFormatDate();
            switch (event) {
                case 'today':
                    dateValue = nowDate + " 00:00:00 - " + nowDate + " 23:59:59";
                    break;
                case 'yesterday':
                    let yesterday = getYesterdayAndFormatDate();
                    dateValue = yesterday + " 00:00:00 - " + yesterday + " 23:59:59";
                    break;
                case 'lastWeek':
                    let lastWeek = getWeekAndFormatDate();
                    dateValue = lastWeek + " 00:00:00 - " + nowDate + " 23:59:59";
                    break;
                case 'lastMonth':
                    let lastMonth = getMouthAndFormatDate();
                    dateValue = lastMonth + " 00:00:00 - " + nowDate + " 23:59:59";
                    break;
                case 'lastYear':
                    let lastYear = getYearAndFormatDate();
                    dateValue = lastYear + " 00:00:00 - " + nowDate + " 23:59:59";
                    break;
            }
            $("#damageReportDate").val(dateValue);
            $("#damageReportSearchBtn").click();
            form.render();
        });
        table.render({
            elem: '#damageReportTable',
            url: 'getDamageReport',
            method: "post",
            response: {
                statusCode: true
            },
            where: {
                searchDate: $("#damageReportDate").val(),
                searchWaresId: $("#damageReportSearchByWaresId").val()
            },
            defaultToolbar: [],
            cellMinWidth: 80,
            cols: [[
                {field: 'sorts', title: '序号', templet: '#sort', width: 58, align: 'center'}
                , {field: 'waresId', title: '商品编码', align: 'center'}
                , {field: 'waresName', title: '商品名', align: 'center'}
                , {field: 'unit', title: '单位', align: 'center', width: 100}
                , {field: 'category', title: '分类', align: 'center', width: 180}
                , {field: 'orderNumber', title: '报损数量', align: 'center'}
                , {
                    field: 'unitPrice', title: '总金额(元)', align: 'center', templet: function (d) {
                        return d.unitPrice.toFixed(2);
                    }
                }
            ]],
            id: 'damageReportRender',
            page: true
        });

        $("#damageReportSearchBtn").on('click', function () {
            let searchDate = $("#damageReportDate").val();
            let searchWaresId = $("#damageReportSearchByWaresId").val();
            getDamageReportEChartsData();
            table.reload('damageReportRender', {
                where: {
                    searchDate: searchDate
                    , searchWaresId: searchWaresId
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });


        function getDamageReportEChartsData() {
            let damageReportPieECharts = echarts.init(document.getElementById("damageReportPieECharts"));
            $.ajax({
                url: 'getChartsData',
                type: "post",
                dataType: "json",
                data: {date: $("#damageReportDate").val()},
                async: false,
                success: function (result) {
                    if (result.code === true) {
                        $("#damageReportCard1").html(result.damageReportCard1 + '单');
                        $("#damageReportCard2").html('￥' + result.damageReportCard2.toFixed(2));

                        let option1 = {
                            tooltip: {
                                trigger: 'item',
                                formatter: '{a} <br/>{b}: {c} 元'
                                // formatter: '{a} <br/>{b}: {c} ({d}%)'
                            },
                            legend: {
                                orient: 'vertical',
                                left: 10,
                            },
                            series: [
                                {
                                    name: '报损总额',
                                    type: 'pie',
                                    radius: ['45%', '70%'],
                                    avoidLabelOverlap: true,//防止重叠
                                    label: {
                                        normal: {
                                            formatter: '{b}:({d}%)',
                                            textStyle: {
                                                fontWeight: 'normal',
                                                fontSize: 15
                                            },
                                        },
                                        emphasis: {
                                            show: true,
                                            textStyle: {
                                                fontSize: '20',
                                                fontWeight: 'bold'
                                            }
                                        }
                                    },
                                    labelLine: {
                                        normal: {
                                            show: true
                                        }
                                    },
                                    data: result.pieData
                                }
                            ]
                        };

                        damageReportPieECharts.setOption(option1);
                    } else {
                        layer.msg(result.msg);
                    }
                },
            });

        }


    });
});